package com.he.architecture.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.he.architecture.Enums.CustomerOrderTypeEnums;
import com.he.architecture.common.entity.LogicEntity;
import com.he.architecture.common.utils.UpdateTool;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Set;

@Entity
@Getter
@Setter
public class Shop extends LogicEntity {
    @Column
    private String shopName;

    //商家和门店的一对多关系
    @ManyToOne(fetch = FetchType.LAZY)
    //防止死循环
    @JsonIgnoreProperties(value = {"shops"})
    private Business business;


    //门店和上架商品的一对多
    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"shop"})
    private Set<Item> items;

    //订单与门店的多对一关系
    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"shop"})
    private Set<CustomerOrder> customerOrders;

    //门店上架商品
    public Set<Item> putAwayItem(Item item) {
        items.add(item);
        setItems(items);
        return items;
    }

    //查看门店订单
    Set<CustomerOrder> listCustomerOrder() {
        //返回门店订单
        return this.customerOrders;
    }

    /**
     * 门店  订单状态修改
     */
    public Set<CustomerOrder> orderTypeChang(CustomerOrder customerOrder, CustomerOrderTypeEnums enums) {
        Set<CustomerOrder> customerOrders = this.customerOrders;
        for (CustomerOrder order : customerOrders) {
            if (order.getId().equals(customerOrder.getId())) {
                order.setOrderType(enums.getCode());
                if (CustomerOrderTypeEnums.valueOf(enums.getCode()) == CustomerOrderTypeEnums.CANCEL) {
                    order.setIsDeleted(1);
                }
            }
        }
        return customerOrders;
    }
}
